import 'package:fish_redux/fish_redux.dart';
import 'package:flutter/material.dart';
import '../../../../app_files.dart';
import 'action.dart';
import 'state.dart';
import '../../widget/main_item_widget.dart';
import '../../model/main_view_model.dart';

Widget buildView(
    SearchResultViewState state, Dispatch dispatch, ViewService viewService) {
  Widget itemBuilder(BuildContext context, int index) {
    if (state.items.length == 0) {
      return Container(
        child: Center(
          child: Stack(
            children: <Widget>[
              MyEmptyItemWidget(),
            ],
          ),
        ),
      );
    }
    if (state.type == 1) {
      LeaderItemModel item = state.items[index];
      return MainItemWidget.leaderBuilder(context, item);
    }
    if (state.type == 3) {
      CommunityItemModel item = state.items[index];
      return MainItemWidget.safeBuilder(context, item);
    }
    if (state.type == 2) {
      CheckListItemModel item = state.items[index];
      return MainItemWidget.communityBuilder(context, item);
    }
    if (state.type == 4) {
      InstallItemModel item = state.items[index];
      return MainItemWidget.installItemBuilder(context, item);
    }

    if (state.type == 6) {
      PatrolAreaModel item = state.items[index];
      return MainItemWidget.patrolBuilder(context, item);
    }
    if (state.type == 7) {
      PatrolOrderItemModel item = state.items[index];
      return MainItemWidget.patrolItemBuilder(context, item);
    }
    if (state.type == 8) {
      FixListItemModel item = state.items[index];
      return MainItemWidget.fixBuilder(context, item);
    }
    if (state.type == 12) {
      FixListItemModel item = state.items[index];
      return MainItemWidget.engineerBuilder(context, item);
    }
    return Container();
  }

  return Scaffold(
    body: GestureDetector(
      child: Container(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Container(
              decoration: BoxDecoration(
                color: ThemeConfig.mainColor,
                border:
                    Border(bottom: BorderSide(color: ThemeConfig.separeColor)),
              ),
              padding: EdgeInsets.only(
                  top: ScreenUtil.statusBarHeight + 12, bottom: 12),
              child: Row(
                children: <Widget>[
                  Padding(
                    padding: EdgeInsets.only(left: 15),
                    child: InkWell(
                      child: Icon(
                        CupertinoIcons.back,
                        color: Colors.white,
                      ),
                      onTap: () => Navigator.pop(viewService.context),
                    ),
                  ),
                  Expanded(
                    child: Hero(
                      tag: state.heroTag,
                      child: Material(
                        color: ThemeConfig.mainColor,
                        child: Container(
                          margin: EdgeInsets.all(15),
                          height: 40,
                          padding: EdgeInsets.only(left: 14),
                          decoration: BoxDecoration(
                            color: Colors.white,
                            borderRadius: BorderRadius.all(
                              Radius.circular(20),
                            ),
                          ),
                          child: Row(
                            children: <Widget>[
                              Padding(
                                padding: EdgeInsets.only(bottom: 1),
                                child: Image.asset('images/icon_search.png'),
                              ),
                              Padding(padding: EdgeInsets.only(left: 8)),
                              Expanded(
                                child: TextField(
                                  style: TextStyle(fontSize: 14),
                                  controller: TextEditingController(
                                      text: state.keyword),
//                          onSubmitted: (String value) => dispatch(
//                              SearchViewActionCreator.onSearch(value)),
                                  textInputAction: TextInputAction.search,
                                  decoration: InputDecoration(
                                    border: InputBorder.none,
                                    hintText: "搜索",
                                    hintStyle: TextStyle(
                                        color: ThemeConfig.black153Color,
                                        fontSize: 14),
                                    contentPadding: EdgeInsets.only(bottom: 9),
                                  ),
                                ),
                              ),
                              InkWell(
                                child: Padding(
                                  padding: EdgeInsets.only(right: 12),
                                  child: Image.asset('images/ic_del.png'),
                                ),
                                onTap: () => Navigator.pop(viewService.context),
                              ),
                            ],
                          ),
                        ),
                      ),
                    ),
                  ),
                ],
              ),
            ),
            Expanded(
              child: Container(
                child: state.items == null
                    ? Center(
                        child: MyLoadingWidget(),
                      )
                    : CustomRefreshList(
                        onLoading: (state.type == 3 || state.type == 1)
                            ? null
                            : (_, page) => dispatch(
                                SearchResultViewActionCreator.onLoad(page)),
                        onRefresh: (_) =>
                            dispatch(SearchResultViewActionCreator.onLoad(1)),
                        currentPage: state.currentPage,
                        refreshController: state.refreshController,
                        child: ListView.builder(
                          itemBuilder: itemBuilder,
                          itemCount:
                              state.items.length == 0 ? 1 : state.items.length,
                          shrinkWrap: true,
                          physics: NeverScrollableScrollPhysics(),
                        ),
                      ),
              ),
            ),
          ],
        ),
      ),
      onTap: () => FocusScope.of(viewService.context).requestFocus(FocusNode()),
    ),
  );
}
